package com.cky.forum.controller;

import com.cky.forum.common.AppResult;
import com.cky.forum.common.ResultCode;
import com.cky.forum.config.AppConfig;
import com.cky.forum.model.Article;
import com.cky.forum.model.Board;
import com.cky.forum.model.User;
import com.cky.forum.services.IArticleService;
import com.cky.forum.services.IBoardService;
import com.sun.istack.NotNull;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IBoardService boardService;
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @RequestParam("boardId") @NotNull Long boardID,
                            @RequestParam("title") @NotNull String title,
                            @RequestParam("content") @NotNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //封装文章对象
        Board board = boardService.selectByPrimaryKey(boardID.longValue());
        if(board == null || board.getDeleteState() == 1 || board.getState() == 1){
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setBoardId(boardID);
        article.setUserId(user.getId());
        articleService.create(article);
        //响应成功
        return AppResult.success();

    }
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId( @RequestParam(value = "boardId",required = false)Long boardId){
        //查询所有
        List<Article> articles;
        if(boardId == null){
            articles = articleService.selectAll();
            //非空判断
            if(articles == null){
                articles = new ArrayList<>();
            }
        }
        else{
            articles = articleService.selectAllByBoardId(boardId);
        }
        if(articles == null){
            articles = new ArrayList<>();
        }

        return AppResult.success(articles);
    }

    @GetMapping("/details")
    public AppResult<Article> getDetails(HttpServletRequest request,@RequestParam("id") @NotNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Article article = articleService.selectDetailById(id);
        if(article == null){
            return  AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if(user.getId() == article.getUserId()){
            article.setOwn(true);
        }
        return  AppResult.success(article);
    }
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,@RequestParam("id") @NotNull Long id,
                            @RequestParam("title") @NotNull String title,@RequestParam("content") @NotNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article = articleService.selectById(id);
        if(article == null){
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if(user.getId() != article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        if(article.getState() == 1 || article.getDeleteState() ==1){
            return AppResult.failed(ResultCode.FAILED);
        }
        articleService.modify(id,title,content);
        log.info("帖子更新成功,article id ="+ id +"user id" + user.getId() + ".");

        return AppResult.success();
    }
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,@RequestParam("id") @NotNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        articleService.thumbsUpById(id);
        return AppResult.success();
    }
    //http://127.0.0.1:58080/article/delete?id=14
    @RequestMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,
                                @RequestParam("id") @NotNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article = articleService.selectById(id);
        if(article == null || article.getDeleteState() == 1){
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        if(user.getId() != article.getUserId()){
            return AppResult.failed(ResultCode.FAILED);
        }
        articleService.deleteById(id);
        return AppResult.success();
    }
    @RequestMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,
                                    @RequestParam(value = "userId",required = false) Long userId){
        if(userId == null){
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute(AppConfig.USER_SESSION);
            userId = user.getId();
        }
        List<Article> articles = articleService.selectByUserId(userId);
        return AppResult.success(articles);
    }
}
